package com.lsa.skienna.challenge.chap4.the110402_StackOfFlapjack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

public class Main implements Runnable{
	private static Pattern WHITESPACE_PATTERN = Pattern.compile(
            "\\p{javaWhitespace}+");
	static String lineSep = System.getProperty("line.separator");
	
	static void process(InputStream in, PrintStream out) throws IOException {
		TaskReader reader = new TaskReader(in);
		List<Integer> task;
		while((task = reader.nextTask()) != null) {
			echo(out, task);
			echo(out, solve(task));
		}
	}
	
	static <T> void echo(PrintStream out, Collection<T> c) {
		for (Iterator<T> iter = c.iterator(); iter.hasNext();) {
			T e = (T) iter.next();
			out.print(e);
			if (iter.hasNext()) {
				out.print(' ');
			}
		}
		out.print(lineSep);
	}
	
	static List<Integer> solve(List<Integer> unsort) {
		int n = unsort.size();
    	List<Integer> sorted = new ArrayList<Integer>();
    	sorted.addAll(unsort);
    	Collections.sort(sorted);
    	
    	List<Integer> solution = new ArrayList<>();
    	
    	// Invariant : 
    	// after one iteration of the cycle, i-th element is at correct position
    	for (int i = sorted.size() - 1; i > -1 ; i--) {
    		if (sorted.get(i) != unsort.get(i)) {
    			// restore the invariant
    			int idx = findIdx(unsort, sorted.get(i));
    			reverse(unsort, idx);
    			if (idx > 0) {
    				solution.add(n - idx);
    			}
    			reverse(unsort, i);
    			solution.add(n - i);
			}
		}
    	
    	solution.add(0);
    	return solution;
    }

	private static int findIdx(List<Integer> task, Integer x) {
		return task.indexOf(x);
	}

	static List<Integer> reverse(List<Integer> l, int pos) {
		for (int i = 0; i <= pos>>1; i++) {
			Integer tmp = l.get(i);
			l.set(i, l.get(pos - i));
			l.set(pos - i, tmp);
		}
		return l;
	}
	
	static class TaskReader{
		private BufferedReader reader;

        TaskReader(InputStream stream) {
            this.reader = new BufferedReader(new InputStreamReader(stream));
        }
        
        List<Integer> nextTask() throws IOException {
        	List<Integer> pancakes = null;
        	String line;
        	if((line = reader.readLine())!= null) {
        		pancakes = new ArrayList<>(32);
        		String[] parts = WHITESPACE_PATTERN.split(line);
        		for (String p : parts) {
					pancakes.add(Integer.parseInt(p));
				}
        	}
        	return pancakes;
        }
	}
	
	public static void main(String[] args) {
		new Main().run();
	}

	@Override
	public void run() {
		try {
			process(System.in, System.out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
